package leetCode.sample;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.junit.Before;
import org.junit.Test;

/**
 * 反转链表
 */
public class ReverseLinkedList {

    private LinkedListNode head;

    @Before
    public void init() {
        head = new LinkedListNode("A");
        LinkedListNode nodeB = new LinkedListNode("B");
        head.setNext(nodeB);
        LinkedListNode nodeC = new LinkedListNode("C");
        nodeB.setNext(nodeC);
        LinkedListNode nodeD = new LinkedListNode("D");
        nodeC.setNext(nodeD);
    }

    @Test
    public void reverse() {
        LinkedListNode next = head.getNext();
        LinkedListNode curr = head;
        while (next != null) {
            if (curr == head) {
                curr.setNext(null);
            }
            LinkedListNode tmp = next.getNext();
            next.setNext(curr);
            curr = next;
            next = tmp;
        }
        while (curr != null) {
            System.out.println(curr.getCode());
            curr = curr.getNext();
        }
    }

}

@Data
class LinkedListNode {
    private String code;
    private LinkedListNode next;

    public LinkedListNode(String code) {
        this.code = code;
    }
}
